<template>
  <div style="padding: 10px">
    <h2>代码生成器</h2>
    <div>
      <el-input v-model="ddl" type="textarea" rows="6" />
      <el-button size="mini" @click="triggerddl2code">转代码</el-button>
    </div>
    <h3>先选中数据库+表名</h3>
    <div>
      tableName: <span>{{ tableName }}</span>
    </div>
    <div>
      columns:
      <pre v-html="columns"></pre>
    </div>
    <erp-divider-card title="生产后端的entity">
      <section>
        <el-button type="text" @click="()=>{
         copyTextToClipboard($refs['entityref'].$el.innerText)
         alertSuccess('复制成功')
     }">复制
        </el-button>
        <entityCom ref="entityref" :columns="columns" :tableName="tableName"></entityCom>
      </section>
    </erp-divider-card>
    <erp-divider-card title="生产后端的service">
      <section>
        <el-button type="text" @click="()=>{
         copyTextToClipboard($refs['serviceref'].$el.innerText)
         alertSuccess('复制成功')
     }">复制
        </el-button>
        <seviceCom ref="serviceref" :columns="columns" :tableName="tableName"></seviceCom>
      </section>
    </erp-divider-card>

    <erp-divider-card title="生产后端的controller">
      <section>
        <el-button type="text" @click="()=>{
         copyTextToClipboard($refs['controllerref'].$el.innerText)
         alertSuccess('复制成功')
     }">复制
        </el-button>
        <controllerCom ref="controllerref" :columns="columns" :tableName="tableName"></controllerCom>
      </section>
    </erp-divider-card>
    <erp-divider-card title="json">
      <section>
        <el-button type="text" @click="()=>{
         copyTextToClipboard($refs['jsonref'].$el.innerText)
         alertSuccess('复制成功')
     }">复制
        </el-button>
        <jsonCom ref="jsonref" :columns="columns" :tableName="tableName"></jsonCom>
      </section>
    </erp-divider-card>
    <erp-divider-card title="前端的listvue">
      <el-button type="text" @click="()=>{
         copyTextToClipboard($refs['tablref'].$el.innerText)
         alertSuccess('复制成功')
     }">复制
      </el-button>
      <tableCom ref="tablref" :columns="columns" :tableName="tableName"></tableCom>
    </erp-divider-card>
    <erp-divider-card title="前端的modifyvue">
      <div>
        <el-button type="text" @click="()=>{
         copyTextToClipboard($refs['modifyref'].$el.innerText)
         alertSuccess('复制成功')
     }">复制
        </el-button>
        <modifyCom ref="modifyref" :columns="columns" :tableName="tableName"></modifyCom>
      </div>
    </erp-divider-card>



  </div>
</template>
<script>
import tableCom from './templates/table.vue';
import modifyCom from './templates/modify.vue';
import axios from 'axios';
import jsonCom from './templates/json.vue';
import entityCom from './templates/entity.vue';
import controllerCom from './templates/controller.vue';
import ErpDividerCard from '@/views/components/erpDividerCard/erpDividerCard.vue';
import { copyTextToClipboard } from '@/utils/clipboard';
import { alertSuccess } from '@base/utils';
import seviceCom from './templates/service.vue';
export default {
  components: {
    seviceCom,
    ErpDividerCard,
    controllerCom,
    entityCom,
    jsonCom,
    tableCom,
    modifyCom,
  },
  data() {
    var ddl = sessionStorage.getItem('codegerenate1_systemgenerete.ddl') || '';
    return {
      ddl: ddl,
      tableName: '',
      columns: [],
    };
  },
  mounted() {
    if (this.ddl) {
      this.triggerddl2code();
    }
  },

  methods: {
    alertSuccess,
    copyTextToClipboard,
    async triggerddl2code() {
      sessionStorage.setItem('codegerenate1_systemgenerete.ddl', this.ddl || '');
      //var result=await axios.post('/api/gerenrate/getscehemasbyddl',{ddl:this.ddl}).then(res=>res.data)
      //console.log("=>[result]", result);
      //自己 splite
      var tableName = '';
      var columns = [];
      var linearrays = this.ddl.toLowerCase().split('\n');
      var tableindex = linearrays[0].indexOf(' table ');
      tableName = (/*获取table 之后的str*/linearrays[0].substring(tableindex + (' table ').length, linearrays[0].length - 1))
          /*clear (*/.replaceAll('(', '')
          /*clear (*/.replaceAll('"', '')
          /*clear (*/.replaceAll('`', '')
          /*clear (*/.replaceAll(' ', '');

      var colorArraylins = linearrays.splice(1, linearrays.length);
      for (let i = 0; i < colorArraylins.length; i++) {
        var _line = colorArraylins?.[i];
        var _line_words = _line?.split(' ')?.filter(a => a);

        if (_line_words?.[0]?.indexOf(')') >= 0||
            _line_words?.[0]?.indexOf('primary') >= 0
        ) {/*退出*/
          continue;
        }
        var colkey = _line_words?.[0]?.replaceAll(/*clear (*/'(', '')
            /*clear (*/.replaceAll('"', '')
            /*clear (*/.replaceAll('`', '')
            /*clear (*/.replaceAll(' ', '');

        var coltype = _line_words?.[1]?.replaceAll(  /*clear (*/'(', '')
            /*clear (*/.replaceAll('"', '')
            /*clear (*/.replaceAll('`', '')
            /*clear (*/.replaceAll(' ', '');

        columns.push({ colkey, coltype });

      }

      this.columns = columns;
      this.tableName = tableName;
    },

  },
};

</script>
